package cqut.test10;

import java.sql.*;
public class ComputerAverPrice {
    public static void main(String args[]) {
        Connection con=null;
        Statement sql;
        ResultSet rs;
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch(Exception e){ }
        try{
            con = DriverManager.getConnection("jdbc:Access://Book.accdb","","");
        }
        catch(SQLException e){
            System.out.println(e);
        }
        try{
            sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            rs=sql.executeQuery("SELECT*FROM bookList");
            rs.last();
            int max = rs.getRow();
            System.out.println("表共有"+max+"条记录,随机抽取 10 条记录：");
            int [] a =GetRandomNumber.getRandomNumber(max,10);
            float sum = 0;
            for(int i:a){
               rs.setFetchDirection(i);
                float price = rs.getFloat(3);
                sum = sum+price;
            }
            con.close();
            System.out.println("平均价格:"+sum/a.length);
        }
        catch(SQLException e) { }
    }
}
